/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

import core.telechargertest;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author phinguyen
 */
public class Servletdownload extends HttpServlet {
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doPost(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       request.setCharacterEncoding("UTF-8");
       response.setContentType("text/html; charset=UTF-8");
       
       String iddocv=request.getParameter("docvselected");
       int id=Integer.parseInt(iddocv);
        System.out.println("id--------------------------------------------------"+id);
       telechargertest tcore= new telechargertest();
       String url=tcore.getURL(id);
       String name=tcore.getTITLE(id);
        System.out.println("url--------------------------------------------------------"+url);
        System.out.println("name--------------------------------------------------------"+name);
        File newfile= new File(url);
        int index=url.lastIndexOf("\\");
        String filename= url.substring(index+1);
        
        if(!newfile.exists()){
//            response.setContentType("text/html; charset=UTF-8");
            response.getWriter().print("This file doesn't exist!");
        }
        long filesize= newfile.length();
        response.addHeader("content-type", "application/pdf");
        response.setHeader("content-disposition", "attachment;filename="+filename);
        response.addHeader("content-length", Long.toString(filesize));
        FileInputStream fin= new FileInputStream(newfile);
        byte[] b= new byte[2048];
        int i=0;
        while((i=fin.read(b))>0){
            response.getOutputStream().write(b);
        }
        fin.close();
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
